package com.greenline.search.test.test.queryanalysis;

import com.greenline.search.test.dao.AnalyzeObject;
import com.greenline.search.test.httprequest.MyHttpRequest;
import com.greenline.search.test.httprequest.ResponseInfo;
import com.greenline.search.test.httprequest.impl.MyHttpClientRequest;
import com.greenline.search.test.utils.FileUtils;
import com.greenline.search.test.utils.JSONStreamParserForAnalyze;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-12-16
 * Time: 下午3:47
 * To change this template use File | Settings | File Templates.
 */
public class TopKeywordAnalysisAdvanced {

    private static Logger logger = LoggerFactory.getLogger(TopKeywordAnalysisAdvanced.class);

    public static void main(String[] args) {
        String classPath = FileUtils.class.getResource("/").getPath();
        String filePath = classPath+"dic/topquery_20140512.txt";
        List<String> keywordList = FileUtils.readFileAsString(filePath, "UTF-8");
        long startTime = System.currentTimeMillis();
        String originalDoctorURL = "http://10.10.100.153:9999/doctor_search?secondsort=0&rows=0&q=";
        String originalHospitalURL = "http://10.10.100.153:9999/hospital_search?rows=0&q=";
        String doctorURL = "http://192.168.1.37:9082/doctor_search?secondsort=0&rows=0&q=";
        String hospitalURL = "http://192.168.1.37:9082/hospital_search?rows=0&q=";

        MyHttpRequest httpRequest = new MyHttpClientRequest();
        System.out.println("request start...");
        StringBuilder doctorSb = new StringBuilder();
        StringBuilder hospitalSb = new StringBuilder();

        int originalDoctorNoResultCount = 0;
        int originalHospitalNoResultCount = 0;
        int doctorNoResultCount = 0;
        int hospitalNoResultCount = 0;

        String tabString = "\t";
        String newlineString = "\n";
        for(int i = 0; i < keywordList.size(); i++){
            if(i % 1000 == 0){
                System.out.println("Deal with " + i + " keywords already.");
            }
            String word = keywordList.get(i).split(tabString)[0];
            System.out.println(word);
            try {
                ResponseInfo originalDoctorInfo = httpRequest.requestAndGetResponseInfo(originalDoctorURL + URLEncoder.encode(word, "UTF-8"));
                ResponseInfo originalHospitalInfo = httpRequest.requestAndGetResponseInfo(originalHospitalURL + URLEncoder.encode(word, "UTF-8"));
                ResponseInfo doctorInfo = httpRequest.requestAndGetResponseInfo(doctorURL + URLEncoder.encode(word, "UTF-8"));
                ResponseInfo hospitalInfo = httpRequest.requestAndGetResponseInfo(hospitalURL + URLEncoder.encode(word, "UTF-8"));

                if(originalDoctorInfo.getReturnCount() <= 0){
                    originalDoctorNoResultCount++;
                }
                if(originalHospitalInfo.getReturnCount() <= 0){
                    originalHospitalNoResultCount++;
                }
                if(doctorInfo.getReturnCount() <= 0){
                    doctorNoResultCount++;
                }
                if(hospitalInfo.getReturnCount() <= 0){
                    hospitalNoResultCount++;
                }
                if(originalDoctorInfo.getReturnCount() > 0 && doctorInfo.getReturnCount() <= 0){
                    doctorSb.append(word).append(newlineString);
                }
                if(originalDoctorInfo.getReturnCount() <= 0 && doctorInfo.getReturnCount() > 0){
                    doctorSb.append(word).append("&&").append(newlineString);
                }
                if(originalHospitalInfo.getReturnCount() > 0 && hospitalInfo.getReturnCount() <= 0){
                    hospitalSb.append(word).append(newlineString);
                }
                if(originalHospitalInfo.getReturnCount() <= 0 && hospitalInfo.getReturnCount() > 0){
                    hospitalSb.append(word).append("&&").append(newlineString);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

        }
        StringBuilder sb = new StringBuilder();
        sb.append("doctor:").append(newlineString).append(doctorSb);
        sb.append("hospital:").append(newlineString).append(hospitalSb);
        sb.append("original doctor no reuslt:").append(originalDoctorNoResultCount).append(newlineString);
        sb.append("original hospital no reuslt:").append(originalHospitalNoResultCount).append(newlineString);
        sb.append("doctor no reuslt:").append(doctorNoResultCount).append(newlineString);
        sb.append("hospital no reuslt:").append(hospitalNoResultCount).append(newlineString);
        logger.info(sb.toString());
        System.out.println("request end ...");
        System.out.println("test end...");
        long costTime = System.currentTimeMillis() - startTime;
        logger.info("The totalTime is " + (costTime/1000) + " seconds.");
    }

}
